;;; Copyright © 2018-2021 Kei Kebreau <kkebreau@posteo.net>
;;;
;;; This program is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (games)
  #:use-module (guix packages)
  #:use-module (gnu packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix utils)
  #:use-module (guix git-download)
  #:use-module (guix build-system cmake)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages curl)
  #:use-module (gnu packages glib)
  #:use-module (gnu packages gtk)
  #:use-module (gnu packages image)
  #:use-module (gnu packages music)
  #:use-module (gnu packages ncurses)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages sdl))

(define-public dopewars
  (package
    (name "dopewars")
    (version "1.5.12")
    (source
     (origin
       (method url-fetch)
       (uri (string-append "mirror://sourceforge/dopewars/dopewars/"
                           version "/dopewars-" version ".tar.gz"))
       (sha256
        (base32 "068cxvz51l9rh8nkn2z9bdg57wvy4dv1vqlf2hmhgilnxb6rs193"))
       (modules '((guix build utils)))
       (snippet
        '(begin
           (substitute* "src/plugins/sound_sdl.c"
             (("SDL_mixer\\.h") "SDL/SDL_mixer.h"))
           #t))))
    (build-system gnu-build-system)
    (arguments
     `(#:configure-flags '("--disable-static")
       #:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'set-score-dir
           (lambda _
             (substitute* "src/dopewars.c"
               (("DPSCOREDIR") "\"~/.local/share/dopewars\""))
             #t))
         (add-after 'install 'wrap-program
           (lambda _
             (let* ((output (assoc-ref %outputs "out"))
                    (dopewars-script (string-append output "/bin/dopewars")))
               (rename-file (string-append output "/bin/dopewars")
                            (string-append output "/bin/.dopewars-real"))
               (call-with-output-file dopewars-script
                 (lambda (port)
                   (format port "#!~a/bin/sh
if [ ! -d ~~/.local/share/dopewars ]; then
  mkdir -p ~~/.local/share/dopewars
  cp ~a/var/dopewars.sco ~~/.local/share/dopewars/dopewars.sco
  chmod -R +w ~~/.local/share/dopewars
fi

exec -a -- ~a/bin/.dopewars-real -f ~~/.local/share/dopewars/dopewars.sco \"$@\"~%"
                         (assoc-ref %build-inputs "bash")
                         output
                         output)))
             (chmod dopewars-script #o555)
             #t))))))
    (native-inputs
     `(("pkg-config" ,pkg-config)))
    (inputs
     `(("glib" ,glib)
       ("gtk+" ,gtk+-2)
       ("ncurses" ,ncurses)
       ("sdl-union" ,(sdl-union (list sdl sdl-mixer)))))
    (home-page "https://dopewars.sourceforge.io/")
    (synopsis "Drug dealing game")
    (description
     "dopewars is a rewrite of a game originally based on \"Drug Wars\" by John
E. Dell.  The idea of dopewars is to deal in drugs on the streets of New York,
amassing a huge fortune and paying off the loan shark, while avoiding the
ever-annoying police.  The rewrite, as well as featuring a so-called
\"antique\" mode which closely follows the original, introduces new features
such as the ability to take part in multi-player games.")
    (license license:gpl2+)))

(define-public srb2
  (package
   (name "srb2")
   (version "2.2.8")
   (source
    (origin
     (method git-fetch)
     (uri (git-reference
           (url "https://github.com/STJr/SRB2.git")
           (commit (string-append "SRB2_release_" version))))
     (sha256
      (base32 "10prk617pbxkpiyybwwjzv425pkjczfqdb8pxwfyq91aa2rg0kl8"))
     (file-name (git-file-name name version))
     (modules '((guix build utils)))
     ;; Remove bundled libraries.
     (snippet
      '(begin
         (delete-file-recursively "libs")
         #t))))
   (build-system cmake-build-system)
   (arguments
    `(#:tests? #f                    ; no check target
      #:configure-flags
      (list
       (string-append "-DGME_INCLUDE_DIR="
                      (assoc-ref %build-inputs "libgme") "/include/gme")
       (string-append "-DSDL2_INCLUDE_DIR="
                      (assoc-ref %build-inputs "sdl2") "/include/SDL2")
       (string-append "-DCMAKE_INSTALL_PREFIX="
                      (assoc-ref %outputs "out") "/bin")
       "-DSRB2_CONFIG_HAVE_OPENMPT=OFF")
      #:phases
      (modify-phases %standard-phases
		     (add-after 'unpack 'unpack-data
				(lambda* (#:key inputs #:allow-other-keys)
				  (let* ((p7zip (assoc-ref inputs "p7zip"))
					 (7z (string-append p7zip "/bin/7z"))
					 (srb2-data (assoc-ref inputs "srb2-data")))
				    (mkdir-p "assets/installer")
				    (and (invoke 7z "e" srb2-data "-oassets/installer"
						 "music.dta" "patch.pk3" "patch_music.pk3"
						 "player.dta" "srb2.pk3" "zones.pk3")))))
		     (add-after 'unpack 'patch-search-locations
				(lambda* (#:key outputs #:allow-other-keys)
				  (let* ((out (assoc-ref outputs "out"))
					 (share (string-append out "/share/srb2")))
				    (substitute* '("src/sdl/i_ttf.c"
						   "src/sdl/i_system.c")
						 (("/usr/local/share/fonts") share)
						 (("/usr/share/fonts") share)
						 (("/usr/local/games/SRB2") share)
						 (("/usr/local/share/games/SRB2") share)
						 (("/usr/share/games/SRB2") share))
				    #t)))
		     (add-after 'install 'move-files
				(lambda* (#:key inputs outputs #:allow-other-keys)
				  (let* ((out (assoc-ref outputs "out"))
					 (bin (string-append out "/bin"))
					 (share (string-append out "/share/srb2")))
				    (mkdir-p share)
				    (chdir (string-append "../srb2-" ,version "-checkout"))
				    (copy-file "src/sdl/srb2.ttf" (string-append share "/srb2.ttf"))
				    (for-each (lambda (file)
						(rename-file (string-append bin "/" file)
							     (string-append share "/" file)))
					      '("music.dta" "player.dta" "srb2.pk3" "zones.pk3"
						"patch.pk3" "patch_music.pk3"))
				    #t))))))
   (native-inputs
    `(("p7zip" ,p7zip)
      ("pkg-config" ,pkg-config)
      ;; This seems to be the best possible way to get the game data.
      ("srb2-data"
       ,(origin
         (method url-fetch)
         (uri
          (string-append
	   "https://github.com/STJr/SRB2/releases/download/SRB2_release_"
	   version "/SRB2-v" version "-Full.zip"))
         (sha256
          (base32 "066p2k0ysl97kkf8pm4p8hrrw9i7b7if77ra8fv2vm3v2aqhaf3s"))))))
   (inputs
    `(("curl" ,curl)
      ("libgme" ,libgme)
      ("libpng" ,libpng)
      ("sdl2" ,sdl2)
      ("sdl2-mixer" ,sdl2-mixer)
      ("zlib" ,zlib)))
   (home-page "https://srb2.org")
   (synopsis "3D Sonic the Hedgehog fangame based on Doom Legacy")
   (description
    "Sonic Robo Blast 2 is a 3D Sonic the Hedgehog fangame built using a
modified version of the Doom Legacy port of Doom.  SRB2 is closely inspired by
the original Sonic games from the Sega Genesis, and attempts to recreate the
design in 3D.  While SRB2 isn't fully completed, it already features tons of
levels, enemies, speed, and quite a lot of the fun that the original Sonic
games provided.")
   (license license:gpl2)))

srb2
